@echo off
title 阿里云ECS文件上传和部署

echo ========================================
echo    阿里云ECS文件上传和部署
echo ========================================
echo.

set /p ecs_ip="请输入ECS公网IP地址: "

echo.
echo 步骤1: 创建远程目录
echo 正在创建 /root/websocket_server 目录...

ssh root@%ecs_ip% "mkdir -p /root/websocket_server"

if %errorlevel% neq 0 (
    echo ❌ 创建目录失败
    pause
    exit /b 1
)

echo ✅ 目录创建成功
echo.

echo 步骤2: 上传部署文件
echo 正在上传文件到ECS实例...

REM 上传主要文件
echo 上传 websocket_server.py...
scp websocket_server.py root@%ecs_ip%:/root/websocket_server/

echo 上传 requirements.txt...
scp requirements.txt root@%ecs_ip%:/root/websocket_server/

echo 上传 deploy_to_aliyun.sh...
scp deploy_to_aliyun.sh root@%ecs_ip%:/root/websocket_server/

echo 上传 monitor_websocket.sh...
scp monitor_websocket.sh root@%ecs_ip%:/root/websocket_server/

echo 上传 patient-websocket.service...
scp patient-websocket.service root@%ecs_ip%:/root/websocket_server/

if %errorlevel% neq 0 (
    echo ❌ 文件上传失败
    pause
    exit /b 1
)

echo ✅ 所有文件上传成功
echo.

echo 步骤3: 验证文件上传
echo 正在验证文件...

ssh root@%ecs_ip% "ls -la /root/websocket_server/"

echo ✅ 文件验证完成
echo.

echo 步骤4: 部署WebSocket服务器
echo 正在部署服务器...

REM 执行部署脚本
ssh root@%ecs_ip% "cd /root/websocket_server && chmod +x deploy_to_aliyun.sh && bash deploy_to_aliyun.sh"

if %errorlevel% neq 0 (
    echo ❌ 部署脚本执行失败
    pause
    exit /b 1
)

echo ✅ 部署脚本执行完成
echo.

echo 步骤5: 检查服务状态
echo 正在检查服务状态...

ssh root@%ecs_ip% "systemctl status patient-websocket.service --no-pager"

echo.

echo 步骤6: 检查端口监听
echo 正在检查端口8080是否监听...

ssh root@%ecs_ip% "netstat -tlnp | grep 8080"

echo.

echo 步骤7: 启动监控脚本
echo 正在启动监控脚本...

ssh root@%ecs_ip% "cd /root/websocket_server && chmod +x monitor_websocket.sh && nohup ./monitor_websocket.sh > /dev/null 2>&1 &"

echo ✅ 监控脚本已启动
echo.

echo 步骤8: 生成部署报告
echo 正在生成部署报告...

echo 阿里云ECS部署完成报告 > DEPLOYMENT_REPORT.txt
echo 部署时间: %date% %time% >> DEPLOYMENT_REPORT.txt
echo ECS实例IP: %ecs_ip% >> DEPLOYMENT_REPORT.txt
echo WebSocket端口: 8080 >> DEPLOYMENT_REPORT.txt
echo 服务名称: patient-websocket.service >> DEPLOYMENT_REPORT.txt
echo. >> DEPLOYMENT_REPORT.txt
echo 管理命令: >> DEPLOYMENT_REPORT.txt
echo - 查看状态: ssh root@%ecs_ip% "systemctl status patient-websocket.service" >> DEPLOYMENT_REPORT.txt
echo - 查看日志: ssh root@%ecs_ip% "journalctl -u patient-websocket.service -f" >> DEPLOYMENT_REPORT.txt
echo - 重启服务: ssh root@%ecs_ip% "systemctl restart patient-websocket.service" >> DEPLOYMENT_REPORT.txt
echo. >> DEPLOYMENT_REPORT.txt
echo 客户端配置: >> DEPLOYMENT_REPORT.txt
echo - WebSocket地址: ws://%ecs_ip%:8080 >> DEPLOYMENT_REPORT.txt
echo - HTTP地址: http://%ecs_ip%:8080 >> DEPLOYMENT_REPORT.txt

echo.
echo ========================================
echo           部署完成！
echo ========================================
echo.
echo 🎉 恭喜！WebSocket服务器已成功部署到阿里云ECS
echo.
echo 部署信息:
echo - ECS实例IP: %ecs_ip%
echo - WebSocket端口: 8080
echo - 服务名称: patient-websocket.service
echo.
echo 下一步操作:
echo 1. 更新Android客户端配置
echo 2. 重新编译APK
echo 3. 测试WebSocket连接
echo.
echo 管理命令:
echo - 查看状态: ssh root@%ecs_ip% "systemctl status patient-websocket.service"
echo - 查看日志: ssh root@%ecs_ip% "journalctl -u patient-websocket.service -f"
echo - 重启服务: ssh root@%ecs_ip% "systemctl restart patient-websocket.service"
echo.
echo 详细报告已保存到: DEPLOYMENT_REPORT.txt
echo.
pause 